﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using FourSquareAPI;
using Microsoft.Win32;

namespace FourSquareWM
{
    public partial class Form1 : Form
    {
        private FourSquare FourSquareConn;
        private string username;
        private string password;

        public Form1()
        {
            InitializeComponent();
            RegistryKey settingsKey = Registry.CurrentUser.CreateSubKey("Software\\thebrent\\PocketNotepad");
            this.username = (string)settingsKey.GetValue("Username");
            this.password = (string)settingsKey.GetValue("Password");
            if (this.password == "")
            {
                formLogin loginForm = new formLogin();
                if (loginForm.ShowDialog() == DialogResult.OK)
                {
                    this.username = loginForm.username;
                    this.password = loginForm.password;
                }
                else
                {
                    Application.Exit();
                }
            }
            FourSquareConn = new FourSquareAPI.FourSquare(this.username, this.password);
        }

        void Form1_Load(object sender, System.EventArgs e)
        {
            GetCheckins();
            this.timerRefresh.Enabled = true;
        }

        private void menuItemExit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void GetCheckins()
        {
            this.listViewCheckins.Items.Clear();
            this.listViewCheckins.Items.Add(new ListViewItem("Loading..."));
            List<checkin> lc = FourSquareConn.GetCheckins();
            this.listViewCheckins.Items.Clear();
            foreach (checkin c in lc)
            {
                this.listViewCheckins.Items.Add(new ListViewItem(c.Display));
            }
        }

        private void listViewCheckins_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void buttonCheckin_Click(object sender, EventArgs e)
        {
            new formSearch(this.FourSquareConn).Show();
        }

        private void timerRefresh_Tick(object sender, EventArgs e)
        {
            GetCheckins();
        }

        private void menuItemRefresh_Click(object sender, EventArgs e)
        {
            this.timerRefresh.Enabled = false;
            GetCheckins();
            this.timerRefresh.Enabled = true;
        }
    }
}